const Mock = require("mockjs")

// 变量转换为Bool
export function tryParseBool(param) {
    if (param === undefined || param === null) {
        return false
    }

    if (typeof param === 'boolean') {
        return param
    }

    if (typeof param === 'string') {
        param = param.trim().toLowerCase()
        return param === 'true' ? true : false
    }

    if (typeof param === 'number') {
        return param > 0 ? true : false
    }
    return false
}

// 生成随机手机号
export function mockPhone(prefix) {
    let len = 11
    const prefixArr = ['192', '180', '165']
    if (prefix == undefined) {
        prefix = prefixArr[Mock.Random.integer(0, prefixArr.length - 1)]
    }
    let phone = prefix
    for (let i = 0; i < len - prefix.length; i++) {
        phone += Mock.Random.integer(0, 9)
    }
    return phone
}

// 数字格式化
export function numberFormat(num) {
    if (isNaN(num)) {
        return 0
    }
    else {
        if (num < 1000) {
            return num
        }
        else if (num < 10000) {
            return parseFloat(num / 1000).toFixed(1).replace('.0', '') + 'k'
        }
        else {
            return parseFloat(num / 10000).toFixed(1).replace('.0', '') + 'w'
        }
    }
}

// 模拟日期
export function mockDate(daysOffset) {
    if (daysOffset == null || daysOffset == undefined) {
        daysOffset = 5
    }
    let date = new Date()
    let i = Mock.Random.integer(0, daysOffset)
    date.setHours(date.getHours() - (i * 24))
    return date
    // let format = dateFormatHasTime(date)
    // return format
}

// 日期格式化
export function dateFormatForList(date) {
    try {
        if (date == undefined || date == null) {
            return ''
        }
        // alert(date)
        date = new Date(date)
        // date = new Date(2023, 11, 25);
        let now = new Date()
        let y = now.getFullYear() - date.getFullYear()
        let d = now.getDate() - date.getDate()
        let h = now.getHours() - date.getHours()
        let m = now.getMonth() - date.getMonth()
        if (y == 0) {
            if (m == 0) {
                if (d == 0) {
                    if (h == 0) {
                        return (now.getMinutes() - date.getMinutes() + 1) + '分钟前'
                    } else if (h < 3) {
                        return h + '小时前'
                    } else {
                        return date.getHours() + ':' + date.getMinutes()
                    }
                } else if (d == 1) {
                    return '昨天'
                } else if (d < 3) {
                    return d + '天前'
                } else {
                    return (date.getMonth() + 1) + '-' + date.getDate()
                }
            } else if (m < 6) {
                return m + '个月前'
            }
        } else {
            return dateFormat(date)
        }
    }
    catch (err){
        alert(err)
        return ''
    }
}
export function dateFormat(date) {
    if (date == undefined || date == null) {
        return ''
    }
    date = new Date(date)
    return date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate()
}

export function scrollToTop(element,speed)
{
    speed = isNaN(speed) ? 5 : speed
    // let doc = document.documentElement
    let scrollHeight = element.scrollHeight
    let clientHeight = element.clientHeight
    let offsetHeight = element.offsetHeight
    let scrollTop = element.scrollTop

    console.log('clientHeight:' + clientHeight)
    console.log('scrollHeight:' + scrollHeight)
    console.log('scrollTop:' + scrollTop)
    console.log('offsetHeight:' + offsetHeight)

    speed = (scrollHeight - (scrollTop + clientHeight)) / 100
    console.log(speed)
    element.scrollTo({
        top: 0,
        left: 0,
        behavior: 'smooth' // 或 'auto'/'instant'
    })
    // let n = setInterval(function () {
    //     let t = parseInt(Math.ceil(element.scrollTop))
    //     scrollHeight = element.scrollHeight
    //     clientHeight = element.clientHeight
    //     // console.log('clientTop:' + t)
    //     if ((t + clientHeight) < scrollHeight) {
    //         t += speed
    //         t = (t + clientHeight) < scrollHeight ? t : scrollHeight - clientHeight
    //         element.scrollTo(0, 0)
    //         // console.log('t:' + t)
    //     }
    //     else {
    //         clearInterval(n)
    //     }
    // }, 1)
}
export function scrollToBottom(element, speed) {
    speed = isNaN(speed) ? 5 : speed
    // let doc = document.documentElement
    let scrollHeight = element.scrollHeight
    let clientHeight = element.clientHeight
    let offsetHeight = element.offsetHeight
    let scrollTop = element.scrollTop

    console.log('clientHeight:' + clientHeight)
    console.log('scrollHeight:' + scrollHeight)
    console.log('scrollTop:' + scrollTop)
    console.log('offsetHeight:' + offsetHeight)

    speed = (scrollHeight - (scrollTop + clientHeight)) / 100
    console.log(speed)
    let n = setInterval(function () {
        let t = parseInt(Math.ceil(element.scrollTop))
        scrollHeight = element.scrollHeight
        clientHeight = element.clientHeight
        // console.log('clientTop:' + t)
        if ((t + clientHeight) < scrollHeight) {
            t += speed
            t = (t + clientHeight) < scrollHeight ? t : scrollHeight - clientHeight
            element.scrollTo(0, t)
            // console.log('t:' + t)
        }
        else {
            clearInterval(n)
        }
    }, 1)
}



// 日期格式化2
export function dateFormatHasTime(date) {
    if (date == undefined || date == null) {
        return ''
    }
    date = new Date(date)
    return date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate() + ' ' + date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds()
}

// 读取 Cookie
export function getCookie(name) {
    const cookies = document.cookie.split(';');
    for (let cookie of cookies) {
        const [key, value] = cookie.trim().split('=');
        if (key === name) return decodeURIComponent(value);
    }
    return null;
}

// 设置 Cookie
export function setCookie(name, value, days = 30) {
    const date = new Date();
    date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000);
    document.cookie = `${name}=${value}; expires=${date.toUTCString()}; path=/`;
}







